use "crps.dta"

xtset ccode year
foreach nvar of varlist odacom_conm-cnaidother_soe_gni_ln{
by ccode (year), sort: gen running_sum_var = sum(`nvar')
by ccode (year): gen running_count_var = sum(!missing(`nvar'))
by ccode (year): gen `nvar'3 = (running_sum_var - L3.running_sum_var)/(running_count_var-L3.running_count_var) if _n > 3
by ccode: replace `nvar'3 = running_sum_var/running_count_var if _n <= 3
drop running_sum_var running_count_var
}
///3-year averages of aid measures

gen chaid_ened=.
replace chaid_ened=1 if cnaidene>0 & cnaidene!=.
replace chaid_ened=0 if cnaidene==0 & cnaidene!=.
///dummy of whether the country received positive amounts of CRPs

bysort ccode: egen running_sum_var = total(chaid_ened) if year<2013
bysort ccode: egen running_count_var = total(!missing(chaid_ened)) if year<2013
gen chaid_ene_prob=running_sum_var/running_count_var
drop running_sum_var running_count_var
gen steel_aidprob=chaid_ene_prob*irst
///instrument: the interaction of Chinese steel production in a given year with recipient country’s probability of receiving CRPs

tabulate year, generate(yr)
drop yr1 

**********************
///Table 1
//Model1
xtreg  f.v2x_horacc cnaidene_gni_ln3  i.year if income_categpry!=4, fe  vce(r)   

//Model2
xtreg  f.v2x_horacc cnaidene_gni_ln3  cnaidother_gni_ln3 odacom_conm_gni_ln3 region_polity i.year if income_categpry!=4, fe  vce(r)   

//Model3
xtivreg2 f.v2x_horacc cnaidother_gni_ln odacom_conm_gni_ln region_polity yr* (cnaidene_gni_ln=l.steel_aidprob) if income_categpry!=4, fe robust first partial(yr*) endog(cnaidene_gni_ln)

//Model4
xtreg  f.v2x_horacc cnaidene_gni_ln3 cnaidother_gni_ln3 odacom_conm_gni_ln3 region_polity gdppcln wdi_gdpgr  ross_oil_netexpc ross_gas_netexpc polity2_stock i.year if income_categpry!=4, fe  vce(r) 

//Model5
xtreg  f.v2x_horacc c.cnaidene_gni_ln3##c.p_polity2 cnaidother_gni_ln3 gdppcln wdi_gdpgr odacom_conm_gni_ln3 ross_oil_netexpc ross_gas_netexpc region_polity polity2_stock i.year if income_categpry!=4, fe  vce(r)   

//Model6
xtreg  f.d.v2x_horacc l.v2x_horacc cnaidene_gni_ln3 cnaidother_gni_ln3 gdppcln wdi_gdpgr odacom_conm_gni_ln3 ross_oil_netexpc ross_gas_netexpc region_polity polity2_stock i.year if income_categpry!=4 , fe vce(r)    

//Model7
xtreg  f.e_dpi_checks cnaidene_gni_ln3 cnaidother_gni_ln3 gdppcln wdi_gdpgr odacom_conm_gni_ln3 ross_oil_netexpc ross_gas_netexpc region_polity polity2_stock i.year if income_categpry!=4 , fe vce(r)  

**********************
///Table 2
//Model1
xtreg  f.v2x_horacc cnaidene_pop_ln3  i.year if income_categpry!=4, fe  vce(r)   

//Model2
xtreg  f.v2x_horacc cnaidene_pop_ln3  cnaidother_pop_ln3 odacom_conm_pop_ln3 region_polity i.year if income_categpry!=4, fe  vce(r)   

//Model3
xtivreg2 f.v2x_horacc cnaidother_pop_ln odacom_conm_pop_ln region_polity yr* (cnaidene_pop_ln=l.steel_aidprob) if income_categpry!=4, fe robust first partial(yr*) endog(cnaidene_pop_ln)

//Model4
xtreg  f.v2x_horacc cnaidene_pop_ln3 cnaidother_pop_ln3 odacom_conm_pop_ln3 region_polity gdppcln wdi_gdpgr  ross_oil_netexpc ross_gas_netexpc polity2_stock i.year if income_categpry!=4, fe  vce(r) 

//Model5
xtreg  f.v2x_horacc c.cnaidene_pop_ln3##c.p_polity2 cnaidother_pop_ln3 gdppcln wdi_gdpgr odacom_conm_pop_ln3 ross_oil_netexpc ross_gas_netexpc region_polity polity2_stock i.year if income_categpry!=4, fe  vce(r)   

//Model6
xtreg  f.d.v2x_horacc l.v2x_horacc cnaidene_pop_ln3 cnaidother_pop_ln3 gdppcln wdi_gdpgr odacom_conm_pop_ln3 ross_oil_netexpc ross_gas_netexpc region_polity polity2_stock i.year if income_categpry!=4 , fe vce(r)    

//Model7
xtreg  f.e_dpi_checks cnaidene_pop_ln3 cnaidother_pop_ln3 gdppcln wdi_gdpgr odacom_conm_pop_ln3 ross_oil_netexpc ross_gas_netexpc region_polity polity2_stock i.year if income_categpry!=4 , fe vce(r)  

**********************
///Table 3
//Model1
xtreg  f.v2x_veracc cnaidene_gni_ln3 cnaidother_gni_ln3 odacom_conm_gni_ln3 region_polity i.year if income_categpry!=4 , fe  vce(r)   

//Model2
xtivreg2  f.v2x_veracc cnaidother_gni_ln  odacom_conm_gni_ln region_polity yr* (cnaidene_gni_ln = l.steel_aidprob) if income_categpry!=4, fe robust first partial(yr*) endog(cnaidene_gni_ln)

//Model3
xtreg  f.v2x_veracc cnaidene_gni_ln3 cnaidother_gni_ln3 odacom_conm_gni_ln3 region_polity gdppcln wdi_gdpgr  ross_oil_netexpc ross_gas_netexpc  polity2_stock i.year if income_categpry!=4 , fe  vce(r) 

//Model4
xtreg  f.d.v2x_veracc l.v2x_veracc cnaidene_gni_ln3 cnaidother_gni_ln3 gdppcln wdi_gdpgr odacom_conm_gni_ln3 ross_oil_netexpc ross_gas_netexpc region_polity polity2_stock i.year if income_categpry!=4, fe vce(r)    

//Model5
xtreg  f.e_polcomp cnaidene_gni_ln3 cnaidother_gni_ln3 gdppcln wdi_gdpgr odacom_conm_gni_ln3 ross_oil_netexpc ross_gas_netexpc region_polity polity2_stock i.year if income_categpry!=4, fe vce(r)  

//Model6
xtreg  f.v2x_veracc cnaidene_pop_ln3 cnaidother_pop_ln3 odacom_conm_pop_ln3 region_polity i.year if income_categpry!=4 , fe  vce(r)   

//Model7
xtivreg2  f.v2x_veracc cnaidother_pop_ln  odacom_conm_pop_ln region_polity yr* (cnaidene_pop_ln = l.steel_aidprob) if income_categpry!=4, fe robust first partial(yr*) endog(cnaidene_pop_ln)

//Model8
xtreg  f.v2x_veracc cnaidene_pop_ln3 cnaidother_pop_ln3 odacom_conm_pop_ln3 region_polity gdppcln wdi_gdpgr  ross_oil_netexpc ross_gas_netexpc  polity2_stock i.year if income_categpry!=4 , fe  vce(r) 

//Model9
xtreg  f.d.v2x_veracc l.v2x_veracc cnaidene_pop_ln3 cnaidother_pop_ln3 gdppcln wdi_gdpgr odacom_conm_pop_ln3 ross_oil_netexpc ross_gas_netexpc region_polity polity2_stock i.year if income_categpry!=4, fe vce(r)    

//Model10
xtreg  f.e_polcomp cnaidene_pop_ln3 cnaidother_pop_ln3 gdppcln wdi_gdpgr odacom_conm_pop_ln3 ross_oil_netexpc ross_gas_netexpc region_polity polity2_stock i.year if income_categpry!=4, fe vce(r)  

**********************
///Table 4
//Model1
xtreg  f.v2x_horacc cnaidene_soe_gni_ln3 cnaidother_soe_gni_ln3 odacom_conm_gni_ln3 region_polity i.year if income_categpry!=4, fe vce(r)   

//Model2
xtreg  f.v2x_veracc cnaidene_soe_gni_ln3 cnaidother_soe_gni_ln3 odacom_conm_gni_ln3 region_polity i.year if income_categpry!=4, fe vce(r)   

//Model3
xtreg  f.v2x_horacc cnaidene_soe_pop_ln3 cnaidother_soe_pop_ln3 odacom_conm_pop_ln3 region_polity i.year if income_categpry!=4, fe vce(r)   

//Model4
xtreg  f.v2x_veracc cnaidene_soe_pop_ln3 cnaidother_soe_pop_ln3 odacom_conm_pop_ln3 region_polity i.year if income_categpry!=4, fe vce(r)   

//Model5
xtreg  f.v2x_horacc cnaidene_nsoe_gni_ln3 cnaidother_nsoe_gni_ln3 odacom_conm_gni_ln3 region_polity i.year if income_categpry!=4, fe vce(r)   

//Model6
xtreg  f.v2x_veracc cnaidene_nsoe_gni_ln3 cnaidother_nsoe_gni_ln3 odacom_conm_gni_ln3 region_polity i.year if income_categpry!=4, fe vce(r)   

//Model7
xtreg  f.v2x_horacc cnaidene_nsoe_pop_ln3 cnaidother_nsoe_pop_ln3 odacom_conm_pop_ln3 region_polity i.year if income_categpry!=4, fe vce(r)   

//Model8
xtreg  f.v2x_veracc cnaidene_nsoe_pop_ln3 cnaidother_nsoe_pop_ln3 odacom_conm_pop_ln3 region_polity i.year if income_categpry!=4, fe vce(r)   

**********************
///Figure 2 data preparation
bysort ccode: egen first_1 = min(year / (cnaidene>0&cnaidene!=.)) 
gen cnene_abmed=cnaidene
replace cnene_abmed=1 if year>=first_1
drop first_1
drop if income_categpry==4 

keep ccode year cnene_abmed cnaidother_pop_ln3 gdppcln wdi_gdpgr odacom_conm_pop_ln3 region_polity v2x_horacc v2x_veracc 
save "forgsp.dta"
